#ifndef _BASE_THREAD_TEST_H_
#define _BASE_THREAD_TEST_H_

#include "Base.Thread.h"
#include <iostream>
#include "Meta.Functor.h"
#include "Meta.Task.h"

#ifdef _POSIX_VERSION
#include "unistd.h"
#elif WIN32
#include "windows.h"
#endif
 
namespace BaseThreadTest
{
	int function(int x)
	{
		return 2 * x;
	}
}

TEST(threadTest, creatingThreadAndExecuting)
{
	using namespace Maha::Base;
	using namespace Maha::Meta;
	using namespace BaseThreadTest;
	
	Functor<int, TYPELIST_1(int)> fctor1(&function);
	
	int result = 0;
	Task<int, Functor<int, TYPELIST_1(int)>, TYPELIST_1(double)> task1(result, fctor1, 100);
	
	Thread thread(task1);
	
	int n = 0;
	while (result != function(100) && n < 1000)
	{
#ifdef _POSIX_VERSION
	usleep(1000);
#elif WIN32
		Sleep(1);
#endif
		++ n;
	}
	
	if (n > 1000)
		GTEST_FAIL();
	
	ASSERT_EQ(result, function(100));
}

#endif // _BASE_THREAD_TEST_H_
